home *** CD-ROM | disk | FTP | other *** search
- Path: INbe.net!user
- From: cbasti@innet.be (Chris Bastiaens)
- Newsgroups: comp.lang.c++,comp.sys.mac.programmer.codewarrior,comp.sys.mac.oop
- Subject: Re: Help needed with Runtime Type Info
- Date: Sat, 06 Jan 1996 20:48:24 +0100
- Organization: Agfa Gevaert, N.V.
- Distribution: world
- Message-ID: <AD1490989668197DC@pool03-12.innet.be>
- References: <pcal-0401962039480001@slip-d8.rdrop.com>
- NNTP-Posting-Host: pool03-12.innet.be
-
- In article <pcal-0401962039480001@slip-d8.rdrop.com>,
- pcal@agora.rdrop.com (Patrick Calahan) wrote:
-
- > I have an abstract class B from which D,E and F are derived
- >
- > I also have an abstract class O from which P, Q and R are derived.
- >
- > Objects derived from O need to perform specific operations given a
- > pair of pointers to B's. The operation to be performed depends on
- > precisely what kind of B's are given to the O.
- >
- > I thought that by simply overloading member function f in the O's,
- > I could achieve this affect nicely - f(D*,D*), f(D*,E*), f(E*,F*)...
- > could be defined to handle the particualr pair-cases appropriately.
- > A member function in O f(B*,B*) would handle all cases not handled
- > specifically by the subclasses.
- >
- > The only problem is figuring out exactly what type of object a B* is
- > pointing to so that the appropriate call to O.f can be made. I.e.,
- > given a pointer to a B, I need to cast it to a D*,E*, or F*.
- >
- > On page 640 of Stroustrup, he says that [in dynamic_cast<t>(v)]
- > "If T is void* then v must be a pointer, and the result value is a
- > pointer to the complete object pointed to by v."
- >
- > So, if I read him correctly,
- >
- > dynamic_cast<void*>(p*)
- >
- > should cast p to a pointer to an object of the most complete type of
- > the object pointed to by p, i.e. cast my B* to an D*, E* or F*.
- > This sounds like what I want.
- >
- > Unfortunately, if I try to compile the following using MetroWerks
- > CodeWarrior 7 (I do have RTTI enabled):
- >
- >
- > some_function(B* b1, B*b2, O* o)
- > {
- >
- > // here I need to figure out what types of objects b1 & b2 really are
- > // and cast the pointers so the correct version of f will be called.
- >
- > o->f( dynamic_cast<void*>(b1), dynamic_cast<void*>(b2) );
- >
- > }
- >
- >
- > b1 and b2 apparently get cast to void*; the compiler returns that
- > f(void*,void*) can't be found.
-
- Patrick,
-
- I don't have Stroustroup's book at hand, but from what I know about RTTI, I
- think code like the following should do what you want:
-
- O::HandleBs(B *b1, B *b2)
- {
- D *d1 = dynamic_cast<D *>(b1), *d2 = dynamic_cast<D *>(b2);
- E *e1 = dynamic_cast<E *>(b1), *e2 = dynamic_cast<E *>(b2);
- F *f1 = dynamic_cast<F *>(b1), *f2 = dynamic_cast<F *>(b2);
-
- if (d1 && d2) f(d1, d2) else
- if (d1 && e2) f(d1, e2) else
- if (d1 && f2) f(d1, f2) else
- if (e1 && d2) f(e1, d2) else
- if (e1 && e2) f(e1, e2) else
- if (e1 && f2) f(e1, f2) else
- if (f1 && d2) f(f1, d2) else
- if (f1 && e2) f(f1, e2) else
- if (f1 && f2) f(f1, f2)
- }
-
- Not that this supposes class hierarchy like this:
-
- B
- /|\
- / | \
- / | \
- D E F
-
- and not, for instance, like
- B
- /|
- / |
- / |
- D E
- |
- F
- because in that case, an F* is also an E*.
-
-
-
-